const MYSQL = require("../../sql/mysql");
// MYSQL数据库配置信息
const MYSQLConfig = require("../../sql/database");
let sqlName = "role_menu";
const DayJs = require("dayjs");
const getAllListModule = require("../../api/menus/list");
const existRole = require("./exist");
/**
 * 添加全部菜单权限
 * 
 * @param {object} params
 * @param {string} params.id 角色id
 * @param {string} params.menu 菜单id
 * @returns {Promise<{code:string,msg:string}>}
 */
module.exports = async (params = {}) => {
  try {
    let { id } = params;
    if (!id) {
      throw { code: 500, msg: "id不能为空" };
    }
    await existRole({ id });
    let menusList = await getAllListModule();
    let list = menusList.data.list;
    let create_date = DayJs(Date.now()).valueOf();
    let addData = list.map((item) => {
      return `(${id}, ${item.id}, ${create_date})`;
    });
    await MYSQL.queryPromise(
      `INSERT  ${sqlName} (role,menu_id,create_date) VALUES ${addData.join(
        ","
      )}`
    );
    return {
      code: 0,
      msg: "添加成功",
    };
  } catch (error) {
    console.log(error);
    throw { code: 0, msg: "添加失败" };
  }
};
